<?php
if(preg_match('#' . basename(__FILE__) . '#', $_SERVER['PHP_SELF'])) { die('You are not allowed to call this page directly.'); }

/**
 * grandMedia()
 *
 * @return mixed content
 */
function grandMedia()  {
	global $gMDb;

	$url = grandCore::getAdminURL();
	$arg = array(
		'mime_type'	=> grandCore::_get('mime_type',''),
		'orderby'	=> grandCore::_get('orderby','ID'),
		'order'		=> grandCore::_get('order',''),
		'limit'		=> '3',
		'filter'	=> grandCore::_get('filter',''),
		's'			=> grandCore::_get('s','')
	);
	$wpMediaLib = $gMDb->get_wp_media_lib($arg);
	extract($arg);
	$media = $mCount = array(
		'all'			=> '',
		'image'			=> '',
		'audio'			=> '',
		'video'			=> '',
		'other'			=> ''
	);
	if(count($wpMediaLib)) {
		foreach($wpMediaLib as $item) {
			$type = explode('/',$item->post_mime_type);
			$row = gmAdmin::wpMediaRow( $item );
			$media[$type[0]] .= $row;
			$mCount[$type[0]]++;
			
			$media['all'] .= $row;
			$mCount['all']++;
		}
	}
	$norows = '<tr class="emptybd"><td colspan="8">'.__('No items in WordPress Media Library.','gmLang').'</td></tr>';
	$nonce = wp_create_nonce( 'wpMediaLib' );
	$gmDbCount = $gMDb->mediaCount($arg);
	foreach($gmDbCount as $key=>$value) {
		if($key == 'hidden') {
			$counting[$key] = '<i class="qty"> (<span class="db">'.$value.'</span>)</i>';
		} else
			$counting[$key] = '<i class="qty"> (<span class="page">'.intval($mCount[$key]).'</span><b>/</b><span class="db">'.$value.'</span>)</i>';
	}
?>
<?php 
$order = grandCore::_get('order','ASC');
$sort = 'ASC';
$url_param['mime_type'] = $mime_type? '&mime_type='.$mime_type : '';
$url_param['orderby'] = '&orderby='.$orderby;
$url_param['order'] = '&order='.$order;
$url_param['filter'] = $filter? '&filter='.$filter : '';
$url_param['s'] = $s? '&s='.$s : '';
$selected_items = isset($_COOKIE['gmedia_selected_items'])? $_COOKIE['gmedia_selected_items'] : '';
?>
<div id="grandMedia" class="grandmedia">
	<div class="grandHeader">
		<div class="grandLogo">GrandMedia</div>
		<h2><?php _e('GRAND Media Library', 'gmLang'); ?></h2>
	</div>
	<?php echo grandCore::sideLinks(); ?>
	<div class="grandLibrary">
		<div id="gm-message"><?php echo grandCore::message(); ?></div>
		<div class="gMediaLibActions">
			<div class="cb abut"><div class="dropbut"><input class="doaction" type="checkbox" /></div>
				<div class="dropbox">
					<span class="all"><?php _e('All', 'gmLang'); ?></span>
					<span class="none"><?php _e('None', 'gmLang'); ?></span>
					<span class="image"><?php _e('Images', 'gmLang'); ?></span>
					<span class="audio"><?php _e('Audio', 'gmLang'); ?></span>
					<span class="video"><?php _e('Video', 'gmLang'); ?></span>
					<span class="reverse" title="<?php _e('Reverse only visible items', 'gmLang'); ?>"><?php _e('Reverse', 'gmLang'); ?></span>
				</div>
			</div>
			<div class="abuts">
				<?php $curr_mime = grandCore::_get('mime_type','all'); ?>
				<a class="all<?php if($curr_mime == 'all') echo ' active'; ?>" rel="all" href="<?php echo $url['page'].$url_param['filter'].$url_param['s']; ?>"><?php _e('All', 'gmLang'); echo $counting['all']; ?></a>
				<a class="image<?php if($curr_mime == 'image') echo ' active'; if(!$gmDbCount['image']) echo ' disabled'; ?>" rel="image" href="<?php echo $url['page'].'&mime_type=image'.$url_param['filter'].$url_param['s']; ?>"><?php _e('Images', 'gmLang'); echo $counting['image']; ?></a>
				<a class="audio<?php if($curr_mime == 'audio') echo ' active'; if(!$gmDbCount['audio']) echo ' disabled'; ?>" rel="audio" href="<?php echo $url['page'].'&mime_type=audio'.$url_param['filter'].$url_param['s']; ?>"><?php _e('Audio', 'gmLang'); echo $counting['audio']; ?></a>
				<a class="video<?php if($curr_mime == 'video') echo ' active'; if(!$gmDbCount['video']) echo ' disabled'; ?>" rel="video" href="<?php echo $url['page'].'&mime_type=video'.$url_param['filter'].$url_param['s']; ?>"><?php _e('Video', 'gmLang'); echo $counting['video']; ?></a>
				<a class="other<?php if($curr_mime == 'other') echo ' active'; if(!$gmDbCount['other']) echo ' disabled'; ?>" rel="other" href="<?php echo $url['page'].'&mime_type=other'.$url_param['filter'].$url_param['s']; ?>"><?php _e('Other', 'gmLang'); echo $counting['other']; ?></a>
				<span class="delete"><?php _e('Delete', 'gmLang'); ?></span>
			</div>
			<div class="more abut"><div class="dropbut"><?php _e('Actions', 'gmLang'); ?></div>
				<div class="dropbox">
				<?php if(!$filter) { ?>
					<span class="hide ajax-post" data-action="gmedia_hidden" data-_ajax_nonce="<?php echo $nonce; ?>" data-form="gMediaForm" data-post="hide=1"><?php _e('Hide', 'gmLang'); ?></span>
				<?php } else { ?>
					<span class="unhide ajax-post" data-action="gmedia_hidden" data-_ajax_nonce="<?php echo $nonce; ?>" data-form="gMediaForm" data-post="hide=0"><?php _e('Unhide', 'gmLang'); ?></span>
				<?php }
				if($counting['hidden']) { 
					if($filter) {
				?>
					<a class="hidden_media active" href="<?php echo $url['page']; ?>"><?php _e('Hidden items', 'gmLang'); echo $counting['hidden']; ?></a>
				<?php } else { ?>
					<a class="hidden_media" href="<?php echo $url['page'].'&filter=hidden'; ?>"><?php _e('Hidden items', 'gmLang'); echo $counting['hidden']; ?></a>
				<?php 
					}
				}
				?>
				</div>
			</div>
			<div class="msg">
				<span class="selectedItems">0</span> <?php _e('selected', 'gmLang'); ?>
				<form id="selectedForm" style="display: none;" action="<?php echo $url['page'].'&filter=selected'; ?>" method="post"><input type="hidden" id="selected_items" name="selected_items" value="<?php echo $selected_items; ?>" /></form>
				<span class="more">&raquo;</span>
				<div class="actions">
					<span id="showSelected"><?php _e('Show only selected items', 'gmLang'); ?></span>
					<span id="clearSelected"><?php _e('Clear selected items', 'gmLang'); ?></span>
				</div>
			</div>
			<?php echo $gMDb->queryPager(); ?>
			<form action="" method="get">
				<div class="gmSearch">
					<?php foreach($_GET as $key=>$value){
						if(in_array($key, array('s','pager'))) continue; ?>
					<input type="hidden" name="<?php echo $key; ?>" value="<?php echo $value; ?>" />
					<?php } ?>
					<span class="loading">Loading... </span>
					<input id="gMediaLibSearch" type="search" name="s" placeholder="<?php _e('Search...', 'gmLang'); ?>" value="<?php echo grandCore::_get('s',''); ?>" />
				</div>
			</form>
		</div>
		<form name="gMediaForm" id="gMediaForm" method="POST"><div id="gMediaLibTable">
			<table class="gMediaLibTable" cellspacing="0">
				<col class="bufer">
				<col class="cb">
				<col class="id">
				<col class="file">
				<col class="type">
				<col class="title">
				<col class="descr">
				<col class="actions">
				<thead>
					<tr>
						<th class="bufer"><span></span></th>
						<th class="cb"><span>#</span></th>
						<th class="id <?php if($orderby == 'ID'){ echo $sort = grandCore::_get('order','DESC'); $sort = ($sort == 'DESC')? 'ASC' : 'DESC'; } ?>"><a href="<?php echo $url['page'].$url_param['mime_type'].'&orderby=ID&order='.$sort.$url_param['filter'].$url_param['s']; $sort = 'ASC'; ?>"><?php _e('ID', 'gmLang'); ?></a></th>
						<th class="file <?php if($orderby == 'filename'){ echo $order; $sort = ($order == 'DESC')? 'ASC' : 'DESC'; } ?>" title="<?php _e('Sort by filename', 'gmLang'); ?>"><a href="<?php echo $url['page'].$url_param['mime_type'].'&orderby=filename&order='.$sort.$url_param['filter'].$url_param['s']; $sort = 'ASC'; ?>"><?php _e('File', 'gmLang'); ?></a></th>
						<th class="type"><span><?php _e('Type', 'gmLang'); ?></span></th>
						<th class="title <?php if($orderby == 'title'){ echo $order; $sort = ($order == 'DESC')? 'ASC' : 'DESC'; } ?>"><a href="<?php echo $url['page'].$url_param['mime_type'].'&orderby=title&order='.$sort.$url_param['filter'].$url_param['s']; $sort = 'ASC'; ?>"><?php _e('Title', 'gmLang'); ?></a></th>
						<th class="descr"><span><?php _e('Description', 'gmLang'); ?></span></th>
						<th class="actions"><span><?php _e('Actions', 'gmLang'); ?></span></th>
					</tr>
				</thead>
				<tbody class="gmLib">
	<?php 
	if($media['all']) {
		echo $media['all'];
	} else {
		echo $norows;
	}
	?>
					<tr class="noitems"><td colspan="8"><?php _e('No results. Type another query.','gmLang'); ?></td></tr>
				</tbody>
			</table>
		</div>
		<?php wp_original_referer_field(true, 'previous'); ?>
		</form>
		<form action="" method="post" style="display: none;">
			<table id="gmedia-edit-inline">
				<tr class="gmedia-edit-row image">
					<td class="colspanchange" colspan="8">
						<fieldset id="gmEdit_">
							<legend><span class="legendID"></span></legend>
							<input name="gmID" type="hidden" value="" />
							<div class="gmImage"><img width="150" height="150" alt="r8_sh1" class="attachment-150x150" src="<?php echo GRAND_URLPATH .'admin/images/blank.gif'; ?>" /><a class="gmImageEdit" href="<?php echo admin_url('media.php?action=edit&attachment_id=') ?>" <?php grandCore::gQTip(__("Edit media by WordPress Media Library.","gmLang")); ?>>Edit</a></div>
							<div class="gmFile row va-t"><span class="label"><?php _e('Filename', 'gmLang'); ?></span><span class="value"></span></div>
							<div class="gmTitle row va-b"><span class="label"><?php _e('Title', 'gmLang'); ?></span><input name="gmTitle" type="text" value="" /></div>
							<div class="gmDescription"><span class="label"><?php _e('Description', 'gmLang'); ?></span><textarea name="gmDescription" rows="4" cols="60"></textarea></div>
							<div class="buttons">
								<input type="button" class="button cancel" value="<?php _e('Cancel', 'gmLang'); ?>" title="<?php _e('Cancel', 'gmLang'); ?>" />
								<input type="submit" class="button save ajax-post" data-action="gmSaveWPmedia" data-_ajax_nonce="<?php echo $nonce; ?>" data-form="gmEdit_" value="<?php _e('Save', 'gmLang'); ?>" title="<?php _e('Save', 'gmLang'); ?>" />
							</div>
						</fieldset>
					</td>
				</tr>
			</table>
		</form>
	</div>
	<div class="tooltip-file-preview"></div>
	<div class="tooltip-flash"></div>
</div>
<?php
}
?>